package com.zj.transactionspringboot01.service.impl;

import com.zj.transactionspringboot01.domain.Myuser;
import com.zj.transactionspringboot01.domain.Teacher;
import com.zj.transactionspringboot01.service.IupdateDao;
import com.zj.transactionspringboot01.service.MyException;
import com.zj.transactionspringboot01.service.YourException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Service
public class BusinessService {
    @Autowired
    private IupdateDao<Myuser> userService;

    @Autowired
    private IupdateDao<Teacher> teacherService;

    public int doPropagation(Teacher teacher, Myuser user) {
        int res = 0;
        res = teacherService.updatePOJO(teacher);
        try {
            res += userService.updatePOJO(user);
        } catch (Exception e) {
            e.printStackTrace();
        }
        int i = 1 / 0;
        return res;
    }
    @Transactional(propagation = Propagation.REQUIRED)
    public int doPropagation1(Teacher teacher, Myuser user) {
        int res = 0;
        res = teacherService.updatePOJO(teacher);
        try {
            res += userService.updatePOJO(user);
        } catch (Exception e) {
            e.printStackTrace();
        }
        int i = 1 / 0;
        return res;
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    public int doPropagation2(Teacher teacher, Myuser user) {
        int res = 0;
        res = teacherService.updatePOJO(teacher);
        try {
            res += userService.updatePOJO(user);
        } catch (Exception e) {
            e.printStackTrace();
        }
        int i = 1 / 0;
        return res;
    }
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public int doPropagation3(Teacher teacher, Myuser user) {
        int res = 0;
        res = teacherService.updatePOJO(teacher);
        try {
            res += userService.updatePOJO(user);
        } catch (Exception e) {
            e.printStackTrace();
        }
        int i = 1 / 0;
        return res;
    }
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public int doPropagation4(Teacher teacher, Myuser user) {
        int res = 0;
        res = teacherService.updatePOJO(teacher);
        try {
            res += userService.updatePOJO(user);
        } catch (Exception e) {
            e.printStackTrace();
        }
        int i = 1 / 0;
        return res;
    }
    @Transactional(propagation = Propagation.MANDATORY)
    public int doPropagation5(Teacher teacher, Myuser user) {
        int res = 0;
        res = teacherService.updatePOJO5(teacher);
        try {
            res += userService.updatePOJO5(user);
        } catch (Exception e) {
            e.printStackTrace();
        }
        int i = 1 / 0;
        return res;
    }

    @Transactional(propagation = Propagation.NESTED)
    public int doPropagation6(Teacher teacher, Myuser user) {
        int res = 0;
        res = teacherService.updatePOJO6(teacher);
        try {
            res += userService.updatePOJO6(user);
        } catch (Exception e) {
            e.printStackTrace();
        }
//        int i = 1 / 0;
        return res;
    }

    @Transactional(propagation = Propagation.NESTED)
    public int doPropagation7(Teacher teacher, Myuser user) {
        int res = 0;
        res = teacherService.updatePOJO7(teacher);
        try {
            res += userService.updatePOJO7(user);
        } catch (Exception e) {
            e.printStackTrace();
        }
        int i = 1 / 0;
        return res;
    }

    @Transactional
    public void doPropagationRollback(Teacher teacher) throws Exception {
        int res = 0;
        res = teacherService.updatePOJO(teacher);
        System.out.println("-------"+res);
//        throw new MyException("HEIHEI");//抛出此异常会导致回滚
        throw new YourException("HEIHEI");//抛出此异常不会导致回滚

    }

}
